﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Automapping;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NHibernate.Linq;

namespace Perpus
{
    public partial class admin_PinjamKonfirmasi : Form
    {
        public admin_PinjamKonfirmasi()
        {
            InitializeComponent();
        }

        string koneksi = MappingDatabase.koneksi;

        private ISessionFactory CreateSessionFactory()
        {
            return Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008.ConnectionString(koneksi))
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<DetailPinjamMap>())
                .BuildSessionFactory();
        }



        private void admin_PinjamKonfirmasi_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dotnetDataSet.Buku' table. You can move, or remove it, as needed.
            this.bukuTableAdapter.Fill(this.dotnetDataSet.Buku);
            // TODO: This line of code loads data into the 'dotnetDataSet.Peminjaman' table. You can move, or remove it, as needed.
            this.peminjamanTableAdapter.Fill(this.dotnetDataSet.Peminjaman);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            var factory = CreateSessionFactory();
            int idpeminjaman = Convert.ToInt32(comboBox1.SelectedValue.ToString());
            int idbuku = Convert.ToInt32(comboBox2.SelectedValue.ToString());

            try
            {
                using (var session = factory.OpenSession())
                {
                    using (var transaction = session.BeginTransaction())
                    {
                        var borrowed = new DetailPinjam
                        {
                            id_peminjaman = session.Get<Peminjaman>(idpeminjaman),
                            kode_buku = session.Get<Buku>(idbuku)
                        };

                        session.Save(borrowed);
                        transaction.Commit();
                        MessageBox.Show("Berhasil!");
                    }
                }
            }
            catch (TransactionException m)
            {
                MessageBox.Show(m.ToString());
            }

        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            //string a;
            var factory = CreateSessionFactory();
            int idBuku = Convert.ToInt32(comboBox2.SelectedValue);
            try
            {
                using (var session = factory.OpenSession())
                {

                    var nama_buku = from buku in session.Query<Buku>()
                                    where buku.kode_buku == idBuku
                                    select buku.nama_buku;

                    string tes = nama_buku.First();
                    judulBuku_Textbox.Text = tes;



                }
            }
            catch
            {

            }

            
            
        }
    }
}
